package com.tduck.cloud.api.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.account.entity.RoleEntity;
import com.tduck.cloud.account.entity.vo.RoleVO;
import com.tduck.cloud.account.service.RoleService;
import com.tduck.cloud.common.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    @GetMapping("/list")
    public Result<Page<RoleVO>> list(Page page) {
        return Result.success(roleService.page(page));
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody RoleVO roleVO){
        RoleEntity roleEntity = new RoleEntity();
        BeanUtil.copyProperties(roleVO,roleEntity);
        log.debug("roleEntity:{}", JSON.toJSONString(roleEntity));
        return Result.success(roleService.saveOrUpdate(roleEntity));
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return Result.success(roleService.removeById(id));
    }
}
